﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RATSharp.Model;
using RATSharp.Rest;
using System.Threading.Tasks;
using RATSharp.Validator;
using RATSharp.Property;

namespace RATSharp.Cal
{
    /// <summary>
    /// Unused By now. In future should represent pick up action.
    /// </summary>
    public class PickUpAction
    {
        /// <summary>
        /// unsused by now
        /// </summary>
        /// <param name="obj">unused</param>
        /// <param name="service">unused</param>
        public static void pickUpAction(RATSharp.Model.Object obj, RestService service)
        {

            //TODO: It should not be possible to pick up objects which are not pickable


            //TODO: it is not possible to make reservations over a day (midnight)
            //need to specify how this actually should behave 
            Reservations reservations;
            bool res = MenuUtilities.WaitForCommTask(service.pickUpObject(obj.id), out reservations);

            if (reservations == null || reservations.Count == 0)
            {
                Console.WriteLine(String.Format("You don't have any reservation nearby. But we can create a new one."));

                DateTime since = DateTime.Now;
                DateTime to;
                do
                {
                    Console.WriteLine("To? (HH:mm)");
                    to = MenuUtilities.GetSimpleInput(new StringInputTimeValidator("HH:mm"));
                } while (!(DateTime.Compare(to, since) > 0));

                reservations = ReservationFactory.createReservations(since, to, 1, obj, PickAction.PICKED);

                Reservations responseRes;
                if (!MenuUtilities.WaitForCommTask(service.addReservation(reservations), out responseRes))
                {
                    return;
                }

                if (responseRes.Count != 0)
                {
                    Console.WriteLine("Following reservation is already created in your term!");
                    Console.WriteLine(responseRes.First());
                    Console.WriteLine("Retry? (yes/no)");
                    MenuUtilities.YES_NO_CHOICE choiceRetry = MenuUtilities.GetSimpleInput(new StringInputEnumValidator<MenuUtilities.YES_NO_CHOICE>());
                    if (choiceRetry != MenuUtilities.YES_NO_CHOICE.yes)
                    {
                        return;
                    }
                }
                else
                {
                    Reservation reservation = reservations.First();
                    if (reservation.userId != Menu.LoggedUser.Id)
                    {
                        //this is not my reservation
                        Console.WriteLine("There is another reservation in progress, you cannot pick this item.");
                        return;
                    }
                    else
                    {

                        //TODO: muze bej i moje rezervace
                        Console.WriteLine("Object was picked up to following reservation:");
                        Console.WriteLine(reservations.First());
                    }
                }

            }


        }
    }
}